#include "TimerInitHelper.h"
#include "BoardCommunicationModule.h"
#include "SimpleCompletion.h"

//Timer0 Config
void App_Timer0Cfg(void){
	stc_tmr0_init_t stcTmr0Init;

    /* Enable AOS clock */
    FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
    /* Timer0 trigger event set */
    //AOS_SetTriggerEventSrc(AOS_TMR0, EVT_SRC_PORT_EIRQ0);

    /* Enable timer0_1 clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_1, ENABLE);
		/* Enable timer0_2 clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR0_2, ENABLE);
	
	  // 计算超时时间
		uint16_t u16Delay;
		uint16_t u16Div;

    /************************* Configure TMR0_1_A***************************/
//    (void)TMR0_StructInit(&stcTmr0Init);
//    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
//    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1;
//    stcTmr0Init.u32Func = TMR0_FUNC_CMP;
//	
//		if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
//        u16Delay = 7U;
//    } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
//        u16Delay = 5U;
//    } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
//               (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
//               (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
//        u16Delay = 3U;
//    } else {
//        u16Delay = 2U;
//    }
//		
//	u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
//    stcTmr0Init.u16CompareValue = ((U1_TIMEOUT_COUNT + u16Div - 1U) / u16Div) - u16Delay;;
//	  // 计算超时时间
//	
//    (void)TMR0_Init(CM_TMR0_1, TMR0_CH_A, &stcTmr0Init);
//    DDL_DelayMS(1U);
//    TMR0_HWStartCondCmd(CM_TMR0_1, TMR0_CH_A, ENABLE);
//    DDL_DelayMS(1U);
//    TMR0_HWClearCondCmd(CM_TMR0_1, TMR0_CH_A, ENABLE);
//    DDL_DelayMS(1U);

    /************************* Configure TMR0_1_B***************************/
    (void)TMR0_StructInit(&stcTmr0Init);
    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1;
    stcTmr0Init.u32Func = TMR0_FUNC_CMP;
	
	  // 计算超时时间
		if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 7U;
    } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 5U;
    } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
        u16Delay = 3U;
    } else {
        u16Delay = 2U;
    }
		
		u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
    stcTmr0Init.u16CompareValue = ((U2_TIMEOUT_COUNT + u16Div - 1U) / u16Div) - u16Delay;;
	  // 计算超时时间
	
    (void)TMR0_Init(CM_TMR0_1, TMR0_CH_B, &stcTmr0Init);
    DDL_DelayMS(1U);
    TMR0_HWStartCondCmd(CM_TMR0_1, TMR0_CH_B, ENABLE);
    DDL_DelayMS(1U);
    TMR0_HWClearCondCmd(CM_TMR0_1, TMR0_CH_B, ENABLE);
    DDL_DelayMS(1U);
		
		/************************* Configure TMR0_2_A***************************/
    (void)TMR0_StructInit(&stcTmr0Init);
    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1;
    stcTmr0Init.u32Func = TMR0_FUNC_CMP;
	
	  // 计算超时时间
		if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 7U;
    } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 5U;
    } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
        u16Delay = 3U;
    } else {
        u16Delay = 2U;
    }
		
		u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
    stcTmr0Init.u16CompareValue = ((U2_TIMEOUT_COUNT + u16Div - 1U) / u16Div) - u16Delay;;
	  // 计算超时时间
	
    (void)TMR0_Init(CM_TMR0_2, TMR0_CH_A, &stcTmr0Init);
    DDL_DelayMS(1U);
    TMR0_HWStartCondCmd(CM_TMR0_2, TMR0_CH_A, ENABLE);
    DDL_DelayMS(1U);
    TMR0_HWClearCondCmd(CM_TMR0_2, TMR0_CH_A, ENABLE);
    DDL_DelayMS(1U);
		
				/************************* Configure TMR0_2_B***************************/
    (void)TMR0_StructInit(&stcTmr0Init);
    stcTmr0Init.u32ClockSrc = TMR0_CLK_SRC_XTAL32;
    stcTmr0Init.u32ClockDiv = TMR0_CLK_DIV1;
    stcTmr0Init.u32Func = TMR0_FUNC_CMP;
	
	  // 计算超时时间
		if (TMR0_CLK_DIV1 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 7U;
    } else if (TMR0_CLK_DIV2 == stcTmr0Init.u32ClockDiv) {
        u16Delay = 5U;
    } else if ((TMR0_CLK_DIV4 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV8 == stcTmr0Init.u32ClockDiv) || \
               (TMR0_CLK_DIV16 == stcTmr0Init.u32ClockDiv)) {
        u16Delay = 3U;
    } else {
        u16Delay = 2U;
    }
		
		u16Div = (uint16_t)1U << (stcTmr0Init.u32ClockDiv >> TMR0_BCONR_CKDIVA_POS);
    stcTmr0Init.u16CompareValue = ((U2_TIMEOUT_COUNT + u16Div - 1U) / u16Div) - u16Delay;;
	  // 计算超时时间
	
    (void)TMR0_Init(CM_TMR0_2, TMR0_CH_B, &stcTmr0Init);
    DDL_DelayMS(1U);
    TMR0_HWStartCondCmd(CM_TMR0_2, TMR0_CH_B, ENABLE);
    DDL_DelayMS(1U);
    TMR0_HWClearCondCmd(CM_TMR0_2, TMR0_CH_B, ENABLE);
    DDL_DelayMS(1U);
}

/** \brief 基本定时器2中断
*
*/
/* INT_SRC_TMR2_1_CMP_A Callback. */
void INT_SRC_TMR2_1_CMP_A_IrqCallback(void) {
  //add your codes here
//	TMR2_Stop(CM_TMR2_1, TMR2_CH_A);
//	TMR2_SetCountValue(CM_TMR2_1, TMR2_CH_A, 0U);
//	TMR2_Start(CM_TMR2_1, TMR2_CH_A);
	ms_cnt += 1;
	if (ms_cnt >= 1000) {
		ms_cnt = 0;
	}
}

/* INT_SRC_TMR2_2_CMP_A Callback. */
void INT_SRC_TMR2_2_CMP_A_IrqCallback(void) {
  //add your codes here
	//TMR2_Stop(CM_TMR2_2, TMR2_CH_A);
	TMR2_SetCountValue(CM_TMR2_2, TMR2_CH_A, 0U);
	TMR2_Start(CM_TMR2_2, TMR2_CH_A);
	completion_done(usart_transport_check_sem);
}
/* 基本定时器2中断 */
// 最大1985ms
uint16_t usart_delay_ms = 5;

void App_Timer2Cfg(void)
{
    stc_tmr2_init_t stcTmr2Init;
    stc_tmr2_pwm_init_t stcPwmInit;

    /* Enable timer2_1 clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR2_1, ENABLE);

    /************************* Configure TMR2_1_A***************************/
    (void)TMR2_StructInit(&stcTmr2Init);
    stcTmr2Init.u32ClockSrc     = TMR2_CLK_PCLK1;
    stcTmr2Init.u32ClockDiv     = TMR2_CLK_DIV8;
    stcTmr2Init.u32Func         = TMR2_FUNC_CMP;
	// 这里是0x3A98U为一个标准毫秒
    stcTmr2Init.u32CompareValue = 0x3A98U;
    (void)TMR2_Init(CM_TMR2_1, TMR2_CH_A, &stcTmr2Init);

    /* Enable the specified interrupts of Timer2. */
    TMR2_IntCmd(CM_TMR2_1, TMR2_INT_MATCH_CH_A, ENABLE);

    /* PWM configuration */
    (void)TMR2_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32StartPolarity = TMR2_PWM_HOLD;
    stcPwmInit.u32StopPolarity = TMR2_PWM_LOW;
    stcPwmInit.u32CompareMatchPolarity = TMR2_PWM_LOW;
    (void)TMR2_PWM_Init(CM_TMR2_1, TMR2_CH_A, &stcPwmInit);
    TMR2_PWM_OutputCmd(CM_TMR2_1, TMR2_CH_A, ENABLE);

    /* Starts Timer2 */
    //TMR2_Start(CM_TMR2_1, TMR2_CH_A);
	/* Config hardware clear */
    TMR2_HWClearCondCmd(CM_TMR2_1, TMR2_CH_A, TMR2_CLR_COND_EVT, ENABLE);
	/* Config hardware start */
    TMR2_HWStartCondCmd(CM_TMR2_1, TMR2_CH_A, TMR2_START_COND_EVT, ENABLE);
	
	/************************* Configure TMR2_2_A***************************/
	/* Enable timer2_2 clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMR2_2, ENABLE);
	
    (void)TMR2_StructInit(&stcTmr2Init);
    stcTmr2Init.u32ClockSrc     = TMR2_CLK_XTAL32;
    stcTmr2Init.u32ClockDiv     = TMR2_CLK_DIV1;
    stcTmr2Init.u32Func         = TMR2_FUNC_CMP;
	// 每毫秒32.768个脉冲。这里取整33，因为这里不用精确时间
	if (usart_delay_ms > 1985){
		usart_delay_ms = 1985;
	}
    stcTmr2Init.u32CompareValue = 0x21U * usart_delay_ms;
    (void)TMR2_Init(CM_TMR2_2, TMR2_CH_A, &stcTmr2Init);

    /* Enable the specified interrupts of Timer2. */
    TMR2_IntCmd(CM_TMR2_2, TMR2_INT_MATCH_CH_A, ENABLE);

    /* PWM configuration */
    (void)TMR2_PWM_StructInit(&stcPwmInit);
    stcPwmInit.u32StartPolarity = TMR2_PWM_HOLD;
    stcPwmInit.u32StopPolarity = TMR2_PWM_LOW;
    stcPwmInit.u32CompareMatchPolarity = TMR2_PWM_LOW;
    (void)TMR2_PWM_Init(CM_TMR2_2, TMR2_CH_A, &stcPwmInit);
    TMR2_PWM_OutputCmd(CM_TMR2_2, TMR2_CH_A, ENABLE);

    /* Starts Timer2 */
    //TMR2_Start(CM_TMR2_2, TMR2_CH_A);
}
